cmake_minimum_required(VERSION 3.22)

project(QtOpenGLDemo VERSION 1.0.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Qt自动化工具
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

# =============================================================================
# 查找Qt（支持Qt5/Qt6动态检测，优先使用Qt6）
# =============================================================================
find_package(Qt6 COMPONENTS Core Widgets OpenGLWidgets QUIET)
if(Qt6_FOUND)
    message(STATUS "Found Qt6: ${Qt6_VERSION}")
    set(QT_VERSION_MAJOR 6)
    set(QT_LIBS Qt6::Core Qt6::Widgets Qt6::OpenGLWidgets)
else()
    find_package(Qt5 COMPONENTS Core Widgets QUIET)
    if(Qt5_FOUND)
        message(STATUS "Found Qt5: ${Qt5_VERSION}")
        set(QT_VERSION_MAJOR 5)
        set(QT_LIBS Qt5::Core Qt5::Widgets)
    else()
        message(FATAL_ERROR "Qt6 or Qt5 not found. Please install Qt and set CMAKE_PREFIX_PATH")
    endif()
endif()

# =============================================================================
# 源文件
# =============================================================================
set(DEMO_SOURCES
    src/main.cpp
    src/MainWindow.cpp
    src/CocosWidget.cpp
)

set(DEMO_HEADERS
    src/MainWindow.h
    src/CocosWidget.h
)

# =============================================================================
# 可执行文件
# =============================================================================
if(APPLE)
    # macOS应用包
    set(MACOSX_BUNDLE_BUNDLE_NAME "QtOpenGLDemo")
    set(MACOSX_BUNDLE_GUI_IDENTIFIER "com.example.qtopengl")

    add_executable(${PROJECT_NAME} MACOSX_BUNDLE
        ${DEMO_SOURCES}
        ${DEMO_HEADERS}
    )
else()
    add_executable(${PROJECT_NAME}
        ${DEMO_SOURCES}
        ${DEMO_HEADERS}
    )
endif()

# =============================================================================
# 包含目录
# =============================================================================
target_include_directories(${PROJECT_NAME} PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/src
)

# =============================================================================
# 链接库
# =============================================================================
target_link_libraries(${PROJECT_NAME} PRIVATE
    ${QT_LIBS}
)

# macOS特定框架（只需OpenGL）
if(APPLE)
    find_library(OPENGL_FRAMEWORK OpenGL)
    target_link_libraries(${PROJECT_NAME} PRIVATE
        ${OPENGL_FRAMEWORK}
    )
endif()

# =============================================================================
# 资源文件处理
# =============================================================================
set(CONTENT_FOLDER ${CMAKE_CURRENT_SOURCE_DIR}/Resources)

if(APPLE)
    # macOS：复制Resources到app bundle
    set_target_properties(${PROJECT_NAME} PROPERTIES
        MACOSX_BUNDLE_INFO_PLIST "${CMAKE_CURRENT_SOURCE_DIR}/Info.plist"
    )
    # 使用POST_BUILD命令复制资源目录
    add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_directory
        ${CONTENT_FOLDER}
        $<TARGET_BUNDLE_CONTENT_DIR:${PROJECT_NAME}>/Resources
    )
elseif(LINUX)
    # Linux：复制Resources到构建目录
    add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_directory
        ${CONTENT_FOLDER}
        $<TARGET_FILE_DIR:${PROJECT_NAME}>/Resources
    )
endif()

# =============================================================================
# 编译选项
# =============================================================================
if(LINUX)
    # Linux特定设置
    target_compile_definitions(${PROJECT_NAME} PRIVATE
        LINUX
    )
endif()

# =============================================================================
# 安装配置（用于打包）
# =============================================================================
install(TARGETS ${PROJECT_NAME}
    BUNDLE DESTINATION .
    RUNTIME DESTINATION bin
)

if(LINUX)
    install(DIRECTORY ${CONTENT_FOLDER}
        DESTINATION share/${PROJECT_NAME}
    )
endif()

# =============================================================================
# 打印配置信息
# =============================================================================
message(STATUS "========================================")
message(STATUS "Project: ${PROJECT_NAME}")
message(STATUS "Qt Version: ${QT_VERSION_MAJOR}")
message(STATUS "Platform: ${CMAKE_SYSTEM_NAME}")
message(STATUS "Build Type: ${CMAKE_BUILD_TYPE}")
message(STATUS "========================================")
